package servlet.role;

import bean.dto.RoleQueryPage;
import bean.dto.RolesDTO;
import bean.pojo.Permissions;
import bean.pojo.Roles;
import bean.vo.PermissionsOption;
import bean.vo.RoleVo;
import bean.vo.RouterVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import commons.baseServlet.BaseServlet;
import commons.format.NumberFormat;
import commons.r.R;
import service.PermissionService;
import service.RolesService;
import service.impl.PermissionServiceImpl;
import service.impl.RolesServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.util.List;


@WebServlet(name = "RoleManagement", value = "/role/*")
public class RoleManagement extends BaseServlet {
    /**
     * 权限分页查询
     *
     * @param request
     * @return
     */
    public R findByPage(HttpServletRequest request) {
        String str_page = request.getParameter("page");
        System.out.println("RoleManagement:page" + str_page);
        String str_limit = request.getParameter("limit");
        System.out.println("RoleManagement:limit" + str_limit);
        RoleQueryPage roleQueryPage = new RoleQueryPage();
        if (str_page.trim().length() > 0) {
            try {
                roleQueryPage.setPage(Integer.parseInt(str_page));
            } catch (Exception e) {
                roleQueryPage.setPage(1);
            }
        }
        //设置每页行数
        if (str_limit.trim().length() > 0) {
            try {
                roleQueryPage.setLimit(Integer.parseInt(str_limit));
            } catch (Exception e) {
                roleQueryPage.setLimit(10);
            }
        }
        //设置查询条件
        roleQueryPage.setRoleName(request.getParameter("roleName"));
        System.out.println("RoleManagement:findByPage:roleQueryPage:" + roleQueryPage);
        RolesService rolesService = new RolesServiceImpl();
        //满足条件的总条数
        Long total = rolesService.findByTotal(roleQueryPage);
        System.out.println("RoleManagement:findByPage:total:" + total);
        //当前页码的数据
        List<Roles> list = null;
        if (total > 0) {
            //当前页码的数据
            list = rolesService.findByPage(roleQueryPage);
            System.out.println("RoleManagement:findByPage:list:" + list.toString());
        }
        //Data?     Long       addData把data HashMap
        R r = R.ok().addData("total", total).addData("list", list);
        return r;
    }


    /**
     * 添加权限信息
     *
     * @param request
     * @return
     */
    public R add(HttpServletRequest request) throws Exception {
        //获取请求流
        BufferedReader bufferedReader = request.getReader();
        String req_data = bufferedReader.readLine();

        RolesDTO roles = JSONObject.parseObject(req_data, RolesDTO.class);
        RolesService rolesService = new RolesServiceImpl();

        boolean update = rolesService.insert(roles);
        R r = !update ? R.error("添加失败") : R.ok();
        return r;
    }

    /**
     * 根据id删除权限信息
     *
     * @param request
     * @return
     */
    public R deleteById(HttpServletRequest request) {
        String id = request.getParameter("roleId");
        System.out.println(id);
        R r;
        Integer roleId = null;
        try {
            roleId = Integer.parseInt(id);
            RolesService rolesService = new RolesServiceImpl();
            boolean update = rolesService.deleteById(roleId);
            r = !update ? R.error("删除失败") : R.ok();
        } catch (Exception e) {
            r = R.error("ID编号不正确");
        }
        return r;
    }

    /**
     * 修改权限信息
     *
     * @param request
     * @return
     */
    public R updateById(HttpServletRequest request) throws Exception {
        //获取请求流
        BufferedReader bufferedReader = request.getReader();
        String req_data = bufferedReader.readLine();
        RolesDTO roles = JSONObject.parseObject(req_data, RolesDTO.class);
        System.out.println(roles.toString());
        RolesService rolesService = new RolesServiceImpl();

        boolean update = rolesService.update(roles);
        R r = !update ? R.error("修改失败") : R.ok();
        return r;
    }

    /**
     * 获取所有角色信息
     *
     * @param request
     * @return
     */
    public R findAllList(HttpServletRequest request) throws Exception {
        //获取请求流
        RolesService rolesService = new RolesServiceImpl();
        List<RoleVo> roleVoList = rolesService.findAllList();
        R r = roleVoList != null ? R.ok(roleVoList) : R.ok("没有角色数据");
        return r;
    }

}
